package com.lw.single;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;

/**
 * @author LW
 * @2020/8/21 23:02
 */
public class LazyManReflect {

    private static boolean lw = false;

    private LazyManReflect() {
        synchronized (LazyManReflect.class){
            if (lw == false){//不通过反编译，找不到这个标志位的
                lw = true;
            }else {
                throw new RuntimeException("不要试图破坏");
            }
//            if (lazyManReflect!=null){
//                throw new RuntimeException("不要试图破坏");
//            }

        }
    }
    private volatile static LazyManReflect lazyManReflect;

    public static LazyManReflect getInstance(){
        if (lazyManReflect==null){
            synchronized (LazyManReflect.class){//这里加锁   则解决了原子性问题
                if (lazyManReflect==null){
                    lazyManReflect = new LazyManReflect();//不是原子性操作

                }
            }
        }
        return lazyManReflect;
    }
    //反射
    public static void main(String[] args) throws Exception {
//        LazyManReflect instance = LazyManReflect.getInstance();//创建对象
        Field lw = LazyManReflect.class.getDeclaredField("lw");
        lw.setAccessible(true);//破坏（设置无障碍）
        Constructor<LazyManReflect> declaredConstructor = LazyManReflect.class.getDeclaredConstructor(null);
        declaredConstructor.setAccessible(true);//这时候，无视了私有

        //newInstance()   无法反省地创建---”枚举“----对象
        LazyManReflect instance1 = declaredConstructor.newInstance();
        lw.set(instance1,false);//再把这个知道的标志位改回去
        LazyManReflect instance = declaredConstructor.newInstance();
        System.out.println(instance);
        System.out.println(instance1);
    }
}
